﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WindowsFormsApplication3
{
    public class Gasolinera
    {
        public String Nombre;
        public String GeoLocalisacion;
        public String PrecioRegular;
        public String PresioSuper;
        public String PresioDisel;
    }
    public class Reporte2
    {
        int size = 0;
        public Gasolinera[] ruta;
        public String[] letras = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", 
                                   "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };
        public Reporte2(int NumeroDeGasolineras) {
            
            this.ruta = new Gasolinera[NumeroDeGasolineras];
        }
        public Reporte2()
        {

            
        }
        public Boolean Add(Gasolinera gasolinera) {
            try
            {
                if (this.ruta != null)
                {
                    Gasolinera[] rutaTemp = new Gasolinera[this.ruta.Length + 1]; ;
                    for (int x = 0; x < this.ruta.Length; x++)
                    {
                        rutaTemp[x] = ruta[x];
                    }

                    rutaTemp[ruta.Length ] = gasolinera;
                    this.ruta = rutaTemp;
                }
                else
                {
                    this.ruta = new Gasolinera[1];
                    this.ruta[0] = gasolinera;
                }
                return true;
            }catch(Exception e){
                return false;
            }
        }
        public Gasolinera Get(int index) {
            return this.ruta[index];
        }
        public int Size(){

            return ruta.Length;
        }
        public String GetLetra(int Index ){
            String Salida = "xxxx";
            if (Index < letras.Length)
            {
                Salida = letras[Index];
            }
            else if (Index < letras.Length*2)
            {
                Salida = letras[0] + letras[Index - letras.Length];
            }
            else if (Index < letras.Length * 3)
            {
                Salida = letras[1] + letras[Index - letras.Length];
            }
            else if (Index < letras.Length * 4)
            {
                Salida = letras[2] + letras[Index - letras.Length];
            }
            return Salida;
        }
    } 
    public class Mapa
    {
        
        public String[] ArrGeolocalisacion;
        public String GetHTMLMapRuta(String[] ArrGeolocalisacion, String NombreRuta)
        {
            String Salida = "";
            if (ArrGeolocalisacion.Length >= 2)
            {
                Salida = Salida + "<!DOCTYPE html>\n" +
                                "<html>\n" +
                                 " <head>" +
                                  "  <meta name=\"viewport\" content=\"initial-scale=1.0, user-scalable=no\">\n" +
                                   " <meta charset=\"utf-8\">\n" +
                                    "<title>" + NombreRuta + "</title>\n" +
                                    "<style>\n" +
                                    "  html, body, #map-canvas {\n" +
                                    "    height: 100%;\n" +
                                    "    margin: 0px;\n" +
                                    "    padding: 0px\n" +
                                    "  }\n" +
                                    "  #panel {\n" +
                                    "    position: absolute;\n" +
                                    "    top: 5px;\n" +
                                    "    left: 50%;\n" +
                                    "    margin-left: -180px;\n" +
                                    "    z-index: 5;\n" +
                                    "    background-color: #fff;\n" +
                                    "    padding: 5px;\n" +
                                    "    border: 1px solid #999;\n" +
                                    "  }\n" +
                                    "</style>\n" +
                                    "<script src=\"https://maps.googleapis.com/maps/api/js?v=3.exp\"></script>\n" +
                                    "<script>\n" +
                                "var directionsDisplay;\n" +
                                "var directionsService = new google.maps.DirectionsService();\n" +
                                "var map;\n" +

                                "function initialize() {\n" +
                                  "directionsDisplay = new google.maps.DirectionsRenderer();\n" +
                                  "var chicago = new google.maps.LatLng(14.6136273,-90.5357498);\n" +
                                  "var mapOptions = {\n" +
                                  "  zoom: 6,\n" +
                                  "  center: chicago\n" +
                                  "}\n" +
                                  "map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);\n" +
                                  "directionsDisplay.setMap(map);\n" +
                                  " var start ='" + ArrGeolocalisacion[0] + "';\n" +
                                  " var end ='" + ArrGeolocalisacion[ArrGeolocalisacion.Length - 1] + "';\n" +
                                  "var waypts = [];\n";

                if (ArrGeolocalisacion.Length > 2)
                {
                    Salida = Salida + "waypts.push(";
                    for (int x = 1; x < ArrGeolocalisacion.Length - 1; x++)
                    {
                        Salida = Salida + "{location:'" + ArrGeolocalisacion[x] + "',stopover:true}";
                        if (x != ArrGeolocalisacion.Length - 2)
                        {
                            Salida = Salida + ",";
                        }//esta comilla hay que automatisarla
                    }

                    Salida = Salida + ");\n";
                }

                Salida = Salida + " var request = {\n" +
                                    "  origin: start,\n" +
                                    "  destination: end,\n" +
                                    "  waypoints: waypts,\n" +
                                   "   optimizeWaypoints: true,\n" +
                                   "   travelMode: google.maps.TravelMode.DRIVING\n" +
                                  "};\n" +
                                  "directionsService.route(request, function(response, status) {\n" +
                                    "if (status == google.maps.DirectionsStatus.OK) {\n" +
                                      "directionsDisplay.setDirections(response);\n" +
                                      "var route = response.routes[0];\n" +
                                      "var summaryPanel = document.getElementById('directions_panel');\n" +
                                      "summaryPanel.innerHTML = '';\n" +

                                      "for (var i = 0; i < route.legs.length; i++) {\n" +
                                       " var routeSegment = i + 1;\n" +
                                      "  summaryPanel.innerHTML += '<b>Route Segment: ' + routeSegment + '</b><br>';\n" +
                                     "   summaryPanel.innerHTML += route.legs[i].start_address + ' to ';\n" +
                                     "   summaryPanel.innerHTML += route.legs[i].end_address + '<br>';\n" +
                                     "   summaryPanel.innerHTML += route.legs[i].distance.text + '<br><br>';\n" +
                                    "  }\n" +
                                   " }\n" +
                                  "});\n" +
                                "}\n" +
                                "google.maps.event.addDomListener(window, 'load', initialize);\n" +
                                   " </script>\n" +
                                  "</head>\n" +
                                  "<body>\n" +
                                  "  <div id=\"map-canvas\" ></div>\n" +
                                  "</body>\n" +
                                "</html>";
            }
            else
            {
                Salida = "<h1>Laruta debe de tener de 2 a mas ubicaciones</h1>";
            }
            return Salida;
        }
        public String GetHTMLMapRutaByGasolinera(Reporte2 reporte, String NombreRuta)
        {
            String Salida = "";
            if (reporte.ruta.Length >= 2)
            {
                Salida = Salida + "<!DOCTYPE html>\n" +
                                "<html>\n" +
                                 " <head>" +
                                  "  <meta name=\"viewport\" content=\"initial-scale=1.0, user-scalable=no\">\n" +
                                   " <meta charset=\"utf-8\">\n" +
                                    "<title>" + NombreRuta + "</title>\n" +
                                    "<style>\n" +
                                    "  html, body, #map-canvas {\n" +
                                    "    height: 100%;\n" +
                                    "    margin: 0px;\n" +
                                    "    padding: 0px\n" +
                                    "  }\n" +
                                    "  #panel {\n" +
                                    "    position: absolute;\n" +
                                    "    top: 5px;\n" +
                                    "    left: 50%;\n" +
                                    "    margin-left: -180px;\n" +
                                    "    z-index: 5;\n" +
                                    "    background-color: #fff;\n" +
                                    "    padding: 5px;\n" +
                                    "    border: 1px solid #999;\n" +
                                    "  }\n" +
                                    "</style>\n" +
                                    "<script src=\"https://maps.googleapis.com/maps/api/js?v=3.exp\"></script>\n" +
                                    "<script>\n" +
                                "var directionsDisplay;\n" +
                                "var directionsService = new google.maps.DirectionsService();\n" +
                                "var map;\n" +

                                "function initialize() {\n" +
                                  "directionsDisplay = new google.maps.DirectionsRenderer();\n" +
                                  "var chicago = new google.maps.LatLng(14.6136273,-90.5357498);\n" +
                                  "var mapOptions = {\n" +
                                  "  zoom: 6,\n" +
                                  "  center: chicago\n" +
                                  "}\n" +
                                  "map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);\n" +
                                  "directionsDisplay.setMap(map);\n" +
                                  " var start ='" + reporte.ruta[0].GeoLocalisacion + "';\n" +
                                  " var end ='" + reporte.ruta[reporte.ruta.Length - 1].GeoLocalisacion + "';\n" +
                                  "var waypts = [];\n";

                if (reporte.ruta.Length > 2)
                {
                    Salida = Salida + "waypts.push(";
                    for (int x = 1; x < reporte.ruta.Length - 1; x++)
                    {
                        Salida = Salida + "{location:'" + reporte.ruta[x].GeoLocalisacion + "',stopover:true}";
                        if (x != reporte.ruta.Length - 2)
                        {
                            Salida = Salida + ",";
                        }//esta comilla hay que automatisarla
                    }

                    Salida = Salida + ");\n";
                }

                Salida = Salida + " var request = {\n" +
                                    "  origin: start,\n" +
                                    "  destination: end,\n" +
                                    "  waypoints: waypts,\n" +
                                   "   optimizeWaypoints: true,\n" +
                                   "   travelMode: google.maps.TravelMode.DRIVING\n" +
                                  "};\n" +
                                  "directionsService.route(request, function(response, status) {\n" +
                                    "if (status == google.maps.DirectionsStatus.OK) {\n" +
                                      "directionsDisplay.setDirections(response);\n" +
                                      "var route = response.routes[0];\n" +
                                      "var summaryPanel = document.getElementById('directions_panel');\n" +
                                      "summaryPanel.innerHTML = '';\n" +

                                      "for (var i = 0; i < route.legs.length; i++) {\n" +
                                       " var routeSegment = i + 1;\n" +
                                      "  summaryPanel.innerHTML += '<b>Route Segment: ' + routeSegment + '</b><br>';\n" +
                                     "   summaryPanel.innerHTML += route.legs[i].start_address + ' to ';\n" +
                                     "   summaryPanel.innerHTML += route.legs[i].end_address + '<br>';\n" +
                                     "   summaryPanel.innerHTML += route.legs[i].distance.text + '<br><br>';\n" +
                                    "  }\n" +
                                   " }\n" +
                                  "});\n" +
                                "}\n" +
                                "google.maps.event.addDomListener(window, 'load', initialize);\n" +
                                   " </script>\n" +
                                  "</head>\n" +
                                  "<body>\n" +
                                  "  <div id=\"map-canvas\"style=\"float:down;width:100%;height:70%;\" ></div>\n" +
                                  "    <div id=\"control_panel\" style=\"float:up;width:100%;height:30%text-align:left;padding-top:20px\">"+
  
		"<table style=\"width:100%\">"+
  "<tr>"+
    "<td>Gasolinera</td>"+
    "<td>Regular</td>	"+	
    "<td>Super</td>"+
    "<td>Disel</td>"+
  "</tr>";
      for(int x=0;x<reporte.ruta.Length;x++){
    Salida=Salida + "<tr>"+
                        "<td>" + reporte.ruta[x].Nombre + " (" + reporte.GetLetra(x) + ")</td>" +
                        "<td>" + reporte.ruta[x].PrecioRegular + "</td>" +
                        "<td>" + reporte.ruta[x].PresioSuper   + "</td>" +
                        "<td>" + reporte.ruta[x].PresioDisel   + "</td>" +
                     "</tr>";
  }
Salida=Salida +"</table>"+
    "</div>"+
                                  "</body>\n" +
                                "</html>";
            }
            else {
                Salida = "<h1>Laruta debe de tener de 2 a mas ubicaciones</h1>";
            }
            return Salida;
        }

        public String GuardarMapa(String contenido, string Path)
        { 
        System.IO.StreamWriter sw = new System.IO.StreamWriter(Path);
        sw.WriteLine(contenido);
        sw.Close();

        return Path;
        }

        public String GenerarReporte1(String[] ArrGeolocalisacion, String NombreRuta, String Path)
        {
            GuardarMapa(GetHTMLMapRuta(ArrGeolocalisacion, NombreRuta), Path);
            return Path;
        }
    }
}
